package com.milk.milkmedia.service;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.milk.milkmedia.bean.Lyric;
import com.milk.milkmedia.bean.MusicInfo;
import com.milk.milkmedia.dao.DBManager;
import com.milk.milkmedia.dao.MusicInfoDB;
import com.milk.milkmedia.ui.HomePageActivity;
import com.milk.milkmedia.ui.MusicListAcitvity;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by ΢�� on 2015/7/18.
 */
public class MusicSearch{

    private Dao<MusicInfoDB,String> iMusicInfoDao;
    static final int MSG_START_SEARCH = 1;
    private ArrayList<MusicInfo> iMusicInfos = new ArrayList<MusicInfo>();
    private ArrayList<Lyric> mLyrics = new ArrayList<>();

    public MusicSearch() {
        try {
            iMusicInfoDao = DBManager.getInstance().getMusicInfoBDHelper().getDao(MusicInfoDB.class);
        } catch (SQLException e) {
            e.printStackTrace();
            Log.e(HomePageActivity.Tag, "serivce建立iMusicInfoDao错误");
        }

    }


    public void run(){
        searchFileInfo();
        saveDataToDataBase();
    }
    /**
     * 查找音频文件和歌词文件的信息
     */
    private void searchFileInfo(){
        File externFile = Environment.getExternalStorageDirectory();
//        String externPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//        String externAbsolutePath = Environment.getExternalStorageDirectory().getPath();
        Scanfile(externFile);
        lyricMatch();

    }


    /**
     * 数据存入数据库
     */
    private void saveDataToDataBase() {
        MusicInfoDB musicInfoDB;
        for(int i=0;i <iMusicInfos.size();i++)
        {
            musicInfoDB  = new MusicInfoDB();
            musicInfoDB.setId(i);
            musicInfoDB.setTitle(iMusicInfos.get(i).getmTitle());
            musicInfoDB.setUrl(iMusicInfos.get(i).getmUrl());
            musicInfoDB.setSize(iMusicInfos.get(i).getmSize());
            musicInfoDB.setLyric_url(iMusicInfos.get(i).getmLyric());
            try {
                iMusicInfoDao.createOrUpdate(musicInfoDB);
            } catch (SQLException e) {
                e.printStackTrace();
                Log.e(HomePageActivity.Tag,"将MusicInfoDB更新至数据库出错");
            }
        }
    }

    private void Scanfile(File file) {
        MusicInfo musicInfo;
        Lyric lyric;
        if (file.isDirectory() == true) {
            File[] files = file.listFiles();
            if (files!=null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i] != null) {
                        Scanfile(files[i]);
                    }
                }
            }
        }

        else {
            if(isMusicFile(file)==true)
            {
                String url = file.toString();
                long  size= file.length();
                String title = file.getName();
                musicInfo = new MusicInfo(title,url,size);
                iMusicInfos.add(musicInfo);
            }
            if(isLrcFile(file)==true)
            {
                String url = file.toString();
                String name = file.getName();
                lyric = new Lyric(name,url);
                Log.e(HomePageActivity.Tag,name + url);
                mLyrics.add(lyric);
            }
        }
    }


    private boolean isMusicFile(File file) {
        String fileName = file.getName();
        if (fileName!=null) {
            if (fileName.endsWith(".mp3") == true) {
                return true;
            }
        }
        return false;
    }
    private boolean isLrcFile(File file){
        String fileName = file.getName();
        if (fileName!=null) {
            if (fileName.endsWith(".lrc") == true) {
                return true;
            }
        }
        return false;
    }

    private void lyricMatch(){
        Log.e(HomePageActivity.Tag,"匹配歌词");
        String musicName,lrcName;
        for (int i =0;i<iMusicInfos.size();i++)
        {
            for (int j =0;j<mLyrics.size();j++){
                musicName = iMusicInfos.get(i).getmTitle().substring(0, iMusicInfos.get(i).getmTitle().length() - 4);
                lrcName = mLyrics.get(j).getmName();
                lrcName = lrcName.substring(0, lrcName.length() - 4);
                if (musicName.equals(lrcName)){
                    Log.e(HomePageActivity.Tag,"歌曲名"+mLyrics.get(j).getmUrl());
                    iMusicInfos.get(i).setmLyric(mLyrics.get(j).getmUrl());
                    break;
                }else {
                    Log.e(HomePageActivity.Tag,"到底执不执行null的赋值");
                    iMusicInfos.get(i).setmLyric("null");
                }
            }
        }
    }
}
